package com.wkc;

import com.wkc.R;

import android.app.Activity;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.media.MediaPlayer.OnErrorListener;
import android.media.MediaPlayer.OnPreparedListener;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.view.animation.AlphaAnimation;
import android.widget.ImageButton;
import android.widget.MediaController;
import android.widget.VideoView;

import wwk.common.widget.CustomDialog;


public class VideoViewActivity extends Activity implements OnCompletionListener,OnErrorListener, OnPreparedListener
{
	public static final String TAG = "VideoViewActivity"; 
	private VideoView videoView; 
	private String videoUrl; 
	private int positionWhenPaused = -1;
	private MediaController mediaController;
	
	private Handler handler=new Handler() 
	{
		public void handleMessage(Message msg)
		{
			super.handleMessage(msg);
			
			CustomDialog.showAlertDialogWithBlock(VideoViewActivity.this,"无法播放此视频");
			VideoViewActivity.this.onBackPressed();
		}
	};
	
	 public void onCreate(Bundle savedInstanceState) 
	 {
         super.onCreate(savedInstanceState);
         requestWindowFeature(Window.FEATURE_NO_TITLE);
         
         //setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
         getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
         
         videoUrl=getIntent().getStringExtra("videoUrl");
         //String title=getIntent().getStringExtra("title");
         
         setContentView(R.layout.video_view_activity);

         AlphaAnimation alpha = new AlphaAnimation(0.5F, 0.5F);
         alpha.setDuration(0); // Make animation instant
         alpha.setFillAfter(true); // Tell it to persist after the animation ends
         // And then on your layout
         
         videoView = (VideoView)findViewById(R.id.videoView);
         
         mediaController = new MediaController(this,true);

         videoView.setMediaController(mediaController);
         
         videoView.setOnPreparedListener(this);
         videoView.setOnCompletionListener(this);
         videoView.setOnErrorListener(this);
	 }
	 
	 public void onStart() 
	 { 
		 super.onStart();

		 videoView.setVideoURI(Uri.parse(videoUrl));
		 videoView.setVideoPath(videoUrl);
		 videoView.start();  
		 
		 videoView.requestFocus(); 
	 }  
	 
	 public void onPause() 
	 { 
		 super.onPause();
		 
		 // Stop video when the activity is pause.
		 positionWhenPaused = videoView.getCurrentPosition();
		 videoView.stopPlayback();  
		 
	 }  
	 
	 public void onResume() 
	 { 
		 super.onResume();
		 
		 // Resume video player 
		 if(positionWhenPaused >= 0) 
		 {
			 videoView.seekTo(positionWhenPaused);
			 positionWhenPaused = -1;
		 }  
	 }

	//from OnCompletionListener
	 @Override
	public void onPrepared(MediaPlayer arg0) 
	 {
		 Log.w(TAG, "onPrepared");
		 
		 View waitingTextView=findViewById(R.id.waitingTextView);
		 waitingTextView.setVisibility(View.GONE);
	}
	 
	//from OnCompletionListener
	@Override 
	public void onCompletion(MediaPlayer arg0) 
	{
		Log.w(TAG, "onCompletion");
		
		 ImageButton playButton = (ImageButton)findViewById(R.id.playButton);
		 playButton.setVisibility(View.VISIBLE);
		 
		 positionWhenPaused = 0;
		 videoView.seekTo(positionWhenPaused);
		 positionWhenPaused = -1;
		 
		 videoView.setVisibility(View.GONE);

		 playButton.setOnClickListener(new OnClickListener()
		 {
			@Override
			public void onClick(View arg0) 
			{
				videoView.setVisibility(View.VISIBLE);
				videoView.start();
				arg0.setVisibility(View.GONE);
			} 
		 });
	}

	//from OnErrorListener
	@Override
	public boolean onError(MediaPlayer arg0, int arg1, int arg2) 
	{
		Log.w(TAG,String.format("onError %d %d",arg1,arg2));
		
		handler.sendEmptyMessage(0);
		return true;
	}
}
